package cn.ivbao.actions;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{
	/**
	 * ajax设置 result返回为stream
	 */
	private InputStream inputStream;
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	/**
	 * 将字符流转换为字符串
	 * @author yukun
	 * @param str
	 */
	 public void toInStream(String str) {
	        try {
	            inputStream = new ByteArrayInputStream(str.getBytes("utf-8"));
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	        }
	 
	 }
	 
	//spring 初始化属性
	public static ApplicationContext webContext = null;
	/**
	 * 获得spring类对象
	 * @author yukun
	 * @param id
	 * @return
	 */
	public static Object getBean(String id)
	  {
	    if (webContext == null)
	    {
	      try
	      {
	        webContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
	      }
	      catch(Exception e)
	      {
	        webContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	      }
	    }
	    return webContext.getBean(id);
	  }


}
